#ifndef DHT11_ESWIN_H
#define DHT11_ESWIN_H

#include "../board/sdk_project_config.h"

// 微秒级延时函数声明
// extern void OS_DelayUs(uint32_t us);

// 宏定义简化引脚操作
#define DHT11_PORT PORTD
#define DHT11_PIN  7

#include <stdint.h>
#include "status.h"

// DHT11 数据结构
typedef struct {
    uint8_t humidity_integer;     // 湿度整数部分
    uint8_t humidity_decimal;     // 湿度小数部分
    uint8_t temperature_integer;  // 温度整数部分
    uint8_t temperature_decimal;  // 温度小数部分
} dht11_data_t;

// 错误代码
typedef enum {
    DHT11_OK = STATUS_SUCCESS,
    DHT11_ERROR_TIMEOUT,
    DHT11_ERROR_CHECKSUM,
    DHT11_ERROR_NO_RESPONSE
} dht11_error_t;

/**
 * @brief 初始化DHT11传感器
 * 
 * @return status_t 状态码
 */
status_t DHT11_Init(void);

/**
 * @brief 读取DHT11温湿度数据
 * 
 * @param data 存储读取结果的结构体指针
 * @return dht11_error_t 错误代码
 */
dht11_error_t DHT11_Read_Data(dht11_data_t *data);

#endif // DHT11_ESWIN_H


#ifdef EXAMPLE
#include "dht11_eswin.h"
#include "stdio.h"

#define DHT11_PORT PORTD
#define DHT11_PIN  6

int main(void) {
    // 初始化DHT11
    if (DHT11_Init() != STATUS_SUCCESS) {
        printf("DHT11 initialization failed!\n");
        return -1;
    }
    
    while (1) {
        dht11_data_t data;
        dht11_error_t err = DHT11_Read_Data(&data);
        
        if (err == DHT11_OK) {
            printf("Humidity: %d.%d%%\n", data.humidity_integer, data.humidity_decimal);
            printf("Temperature: %d.%d°C\n", data.temperature_integer, data.temperature_decimal);
        } else {
            printf("DHT11 read error: ");
            switch (err) {
                case DHT11_ERROR_TIMEOUT: printf("Timeout\n"); break;
                case DHT11_ERROR_CHECKSUM: printf("Checksum error\n"); break;
                case DHT11_ERROR_NO_RESPONSE: printf("No response\n"); break;
                default: printf("Unknown error\n");
            }
        }
        
        OS_DelayMs(2000); // 2秒读取一次
    }
}
#endif